const {
  findGoodsListService,
  findGoodsCategoryListService,
  findGoodsByCategoryIdService,
  findGoodsDetailIdService
} = require("../../service/goods");
const { ErrorRes, SuccessRes } = require("../../res-model/index");
const {
  findGoodsListFailInfo,
  findGoodsCategoryListFailInfo,
  deleteGoodsFailInfo,
  findGoodsDetailInfo,
} = require("../../res-model/failInfo/goods");
/**
 * 查询所有产品
 */
async function findGoodsList() {
  let sites;
  try {
    sites = await findGoodsListService();
  } catch (ex) {
    console.error("获取产品列表失败", ex);
    return new ErrorRes(findGoodsListFailInfo); // 数据库错误
  }
  // 查询失败
  if (sites == null)
    return new ErrorRes(findGoodsListFailInfo, "获取产品列表失败");

  // 查询成功
  return new SuccessRes(sites);
}
/**
 * 查询所有产品分类
 */
async function findGoodsCategoryList() {
  let sites;
  try {
    sites = await findGoodsCategoryListService();
  } catch (ex) {
    console.error("获取产品分类列表失败", ex);
    return new ErrorRes(findGoodsCategoryListFailInfo); // 数据库错误
  }
  // 查询失败
  if (sites == null)
    return new ErrorRes(findGoodsCategoryListFailInfo, "获取产品分类列表失败");

  // 查询成功
  return new SuccessRes(sites);
}

async function findGoodsByCategoryId(id) {
  let goodInfo;
  try {
    goodInfo = await findGoodsByCategoryIdService(id);
  } catch (ex) {
    console.error("根据分类Id获取产品列表失败", ex);
    return new ErrorRes(findGoodsDetailInfo); // 数据库错误
  }
  // 查询失败
  if (goodInfo == null)
    return new ErrorRes(findGoodsDetailInfo, "根据分类Id获取产品列表失败");

  // 查询成功
  return new SuccessRes(goodInfo);
}

/**
 * 查询所有产品详情
 */
async function findGoodsDetailList(id) {
  let sites;
  try {
    sites = await findGoodsDetailIdService(id);
  } catch (ex) {
    console.error("获取产品详情失败", ex);
    return new ErrorRes(findGoodsCategoryListFailInfo); // 数据库错误
  }
  // 查询失败
  if (sites == null)
    return new ErrorRes(findGoodsCategoryListFailInfo, "获取产品详情失败");

  // 查询成功
  return new SuccessRes(sites);
}

async function findGoodsDetailId(id){
  let detailInfo;
  try {
    detailInfo = await findGoodsDetailIdService(id);
  } catch (ex) {
    console.error("根据id获取产品展示详情", ex);
    return new ErrorRes(findGoodsDetailInfo); // 数据库错误
  }
  // 查询失败
  if (detailInfo == null)
    return new ErrorRes(findGoodsDetailInfo, "根据id获取产品展示详情");

  // 查询成功
  return new SuccessRes(detailInfo);
}

module.exports = {
  findGoodsList,
  findGoodsCategoryList,
  findGoodsByCategoryId,
  findGoodsDetailList,
  findGoodsDetailId,
};
